package com.hjqjl.whdemo.feature.feature0.ui.activity.list;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ExpandableListView;

import com.hjqjl.whdemo.base.BaseActivity;
import com.hjqjl.whdemo.feature.feature0.adapter.MyExpandableListViewAdapter;
import com.hjqjl.whdemo.databinding.ActivityMyExpandableListviewBinding;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyExpandableListViewActivity extends BaseActivity {
    public static void actionStart(Context context) {
        Intent intent = new Intent(context, MyExpandableListViewActivity.class);
        context.startActivity(intent);
    }

    private ActivityMyExpandableListviewBinding binding;
    private List<String> parentData;
    private Map<String, List<String>> childDataMap;

    @Override
    protected void initData() {
        binding = ActivityMyExpandableListviewBinding.inflate(getLayoutInflater());
        View view = binding.getRoot();
        setContentView(view);

        parentData = new ArrayList<>();
        childDataMap = new HashMap<>();

        addInfo("1、111111111？", new String[]{"11111111111"});
        addInfo("2、222222？", new String[]{"222", "22222", "2222"});
        addInfo("3、3333333333？", new String[]{"3333", "3333333"});
        addInfo("4、444444444444？", new String[]{"44444", "44444"});
        addInfo("5、5555555555？", new String[]{"555555555"});

        binding.exLvMyExpandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {

            @Override
            public void onGroupExpand(int groupPosition) {
                for (int i = 0, count = binding.exLvMyExpandableListView.getExpandableListAdapter().getGroupCount(); i < count; i++) {
                    if (groupPosition != i) {// 关闭其他分组
                        binding.exLvMyExpandableListView.collapseGroup(i);
                    }
                }
            }
        });
    }

    @Override
    protected void processLogic() {
        binding.exLvMyExpandableListView.setAdapter(new MyExpandableListViewAdapter(pActivity, parentData, childDataMap));
    }

    /**
     * 给组、子列表添加数据
     *
     * @param p -parentData
     * @param c -child
     */
    private void addInfo(String p, String[] c) {
        parentData.add(p);
        List<String> childItem = new ArrayList<>();
        Collections.addAll(childItem, c);
        childDataMap.put(p, childItem);
    }
}
